<?php
class UserController extends Zend_Controller_Action {
	// Mảng tham số nhận được ở mỗi Action:
	protected $_arrParam;
	private $USER_ID = 0;
	private $URI = "/";
	private $URL_ACC_LOGIN = "/account/login/";
	private $DEFAULT_PROFILE_PICTURE = "default/img/no_image.jpg";
	private $ALERT_TYPE_ERROR = "alert-error";
	private $ALERT_TYPE_SUCCESS = "alert-success";
	public function init() {
		$this->_arrParam = $this->_request->getParams ();
		// Truyền ra view
		$this->view->arrParam = $this->_arrParam;
		
		// Xu li cho phan header
		$this->URI = $this->getRequest ()->getScheme () . "://" . $this->getRequest ()->getHttpHost () . $this->getRequest ()->getRequestUri ();
		$mAccount = new Default_Model_Account ();
		if (! $mAccount->hasIdentity ()) {
			$this->_redirect ( $this->URL_ACC_LOGIN . '?return_url=' . $this->URI );
		} else {
			// get user info
			$this->USER_ID = $mAccount->getUserId ();
			
			$this->view->fullname = $mAccount->getFullName ();
			$this->view->return_url = $this->URI;
			$this->view->userid = $mAccount->getUserId ();
			$strAvatar = $mAccount->getProfilePicture ();
			if ($strAvatar != "") {
				$this->view->my_avatar = $mAccount->getProfilePicture ();
			} else {
				$this->view->my_avatar = $this->DEFAULT_PROFILE_PICTURE;
			}
			// get report
			$mRep = new Default_Model_Report();
			$arrRep = $mRep->listAllReport();
			$this->view->arrRep = $arrRep;
		}
		// End xu li cho phan header
	}
	public function indexAction() {
		$mUser = new Default_Model_User ();
		if ($this->_request->isGet ()) {
			// check is set role
			if (isset ( $_GET ['role'] ) && ($_GET ['role'] == 0 || $_GET ['role'] == 1)) {
				$arrParam = array (
						'role' => $_GET ['role'] 
				);
				if ($mUser->updateUserInfo ( $arrParam, $_GET ['user_id'] )) {
					$this->view->display_alert = 1;
					$this->view->alert_type = $this->ALERT_TYPE_SUCCESS;
					$this->view->alert_message = 'Chỉnh sửa thông tin thành viên thành công!';
				} else {
					$this->view->display_alert = 1;
					$this->view->alert_type = $this->ALERT_TYPE_ERROR;
					$this->view->alert_message = '<strong>Error!</strong> ' . $mUser->getError ();
				}
			} else if (isset ( $_GET ['delete'] ) && $_GET ['delete'] == 1) {
				if (isset ( $_GET ['user_id'] )) {
					// delete user
					$mProcess = new Default_Model_YourTourProcess ();
					$strMessReturn = $mProcess->deleteUserInformation ( $_GET ['user_id'] );
					if ($strMessReturn == "") {
						$this->view->display_alert = 1;
						$this->view->alert_type = $this->ALERT_TYPE_SUCCESS;
						$this->view->alert_message = 'Xoá thông tin thành viên thành công!';
					} else {
						$this->view->display_alert = 1;
						$this->view->alert_type = $this->ALERT_TYPE_ERROR;
						$this->view->alert_message = '<strong>Error!</strong> ' . $strMessReturn;
					}
				} else {
					$this->view->display_alert = 1;
					$this->view->alert_type = "";
					$this->view->alert_message = '<strong>Warning!</strong> Thiếu thông tin để xoá.';
				}
			}
		}
		// process to display view
		$this->view->total_user = $mUser->countUser ();
		$this->view->my_user_id = $this->USER_ID;
		$arrUser = $mUser->listAllUser ();
		if (count ( $arrUser ) != 0) {
			// paging
			$adapter = new Zend_Paginator_Adapter_DbSelect ( $arrUser );
			$paginator = new Zend_Paginator ( $adapter );
			$paginator->setItemCountPerPage ( 100 );
			$paginator->setPageRange ( 3 );
			$currentPage = $this->_request->getParam ( 'page', 1 );
			$paginator->setCurrentPageNumber ( $currentPage );
			$this->view->arrUser = $paginator;
		}
	}
	public function changepwdAction() {
		$mUser = new Default_Model_User ();
		if ($this->_request->isPost ()) {
			if (isset ( $_POST ['oldpwd'] ) && $_POST ['oldpwd'] != '' && isset ( $_POST ['newpwd'] ) && $_POST ['newpwd'] != "" && isset ( $_POST ['renewpwd'] ) && $_POST ['renewpwd'] != "") {
				
				// check pwd
				$arrUserInfo = $mUser->checkPwd($this->USER_ID, $_POST ['oldpwd']);
				if (count($arrUserInfo) != 0) {
					if($_POST ['newpwd'] == $_POST ['renewpwd']) {
						// update user info
						$arrParam = array(
								'password' => md5($_POST ['newpwd']),
								'updated_time' => date('Y-m-d H:i:s')
						);
						if($mUser->updateUserInfo($arrParam, $this->USER_ID)) {
							$this->view->display_alert = 1;
							$this->view->alert_type = $this->ALERT_TYPE_SUCCESS;
							$this->view->alert_message = 'Đổi mật khẩu thành công!';
						} else {
							$this->view->display_alert = 1;
							$this->view->alert_type = $this->ALERT_TYPE_ERROR;
							$this->view->alert_message = '<strong>Error!</strong> '.$mUser->getError();
						}
					} else {
						$this->view->display_alert = 1;
						$this->view->alert_type = $this->ALERT_TYPE_ERROR;
						$this->view->alert_message = '<strong>Error!</strong> Mật khẩu mới nhập vào không khớp.';
					}
				} else {
					$this->view->display_alert = 1;
					$this->view->alert_type = $this->ALERT_TYPE_ERROR;
					$this->view->alert_message = '<strong>Error!</strong> Mật khẩu cũ sai.';
				}
			} else {
				$this->view->display_alert = 1;
				$this->view->alert_type = "";
				$this->view->alert_message = '<strong>Warning!</strong> Thiếu thông tin để đổi mật khẩu.';
			}
		}
	}
}